package com.ntd.study.pro.zx.wlpt;


import java.io.*;
import java.text.SimpleDateFormat;

/**
 * A set of utility methods to help produce consistent Object#equals(Object) and
 * Object#hashCode methods.
 * 
 * @author <a href="mailto:maliangyou at 163.com">Ma Liang You</a>
 * 
 * @since 1.0
 */
public class CommonUtils
{
	public static final int HASH_SEED = 17;
	public static final int HASH_OFFSET = 37;

	private CommonUtils()
	{
		super();
	}

	public static int hashCode(final int seed, final int hashcode)
	{
		return seed * HASH_OFFSET + hashcode;
	}

	public static int hashCode(final int seed, final Object obj)
	{
		return hashCode(seed, obj != null ? obj.hashCode() : 0);
	}

	public static int hashCode(final int seed, final boolean b)
	{
		return hashCode(seed, b ? 1 : 0);
	}

	public static boolean equals(final Object obj1, final Object obj2)
	{
		return obj1 == null ? obj2 == null : obj1.equals(obj2);
	}

	public static String getMicroTimestamp()
	{
		long startDate;
		long startNanoseconds;
		SimpleDateFormat dateFormat;

		startDate = System.currentTimeMillis();
		startNanoseconds = System.nanoTime();
		dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

		long microSeconds = (System.nanoTime() - startNanoseconds) / 1000;
		long date = startDate + (microSeconds / 1000);
		return dateFormat.format(date) + String.format("%03d", microSeconds % 1000);
	}

	/**
	 * ������ļ����ֽ�����
	 * 
	 * @return byte[]
	 * @throws FileNotFoundException 
	 * @throws IOException 
	 */
	public static byte[] fileToBytes(String filename) throws FileNotFoundException, IOException  
	{
		FileInputStream fis = null;
		byte[] filecontent = null;
		try
		{
			fis = new FileInputStream(filename);
			filecontent = new byte[fis.available()];
			fis.read(filecontent);
		}
		finally
		{
			if (fis != null) fis.close();
		}
		return filecontent;
	}

	public static void dumpStream(String header, InputStream instream) throws IOException
	{
		StringBuffer buffer = new StringBuffer();
		int ch;
		while ((ch = instream.read()) != -1)
		{
			if (ch == 13)
			{
				buffer.append("[\\r]");
			}
			else if (ch == 10)
			{
				buffer.append("[\\n]\"");
				buffer.insert(0, "\"");
				buffer.insert(0, header);
				System.out.println(buffer.toString());
				buffer.setLength(0);
			}
			else if ((ch < 32) || (ch > 127))
			{
				buffer.append("[0x");
				buffer.append(Integer.toHexString(ch));
				buffer.append("]");
			}
			else
			{
				buffer.append((char) ch);
			}
		}
		if (buffer.length() > 0)
		{
			buffer.append("\"");
			buffer.insert(0, "\"");
			buffer.insert(0, header);
			System.out.println(buffer.toString());
		}
	}

	public static String streamReadLineDump(InputStream instream) throws IOException
	{
		StringBuffer buffer1 = new StringBuffer();
		StringBuffer buffer2 = new StringBuffer();
		int ch;
		while ((ch = instream.read()) != -1)
		{
			if (ch == 13)
			{
				buffer2.append((char) ch);
			}
			else if (ch == 10)
			{
				buffer2.append((char) ch);
				dumpStream("<< ", new ByteArrayInputStream(String.valueOf(buffer2).getBytes()));
				buffer2.setLength(0);
				break;
			}
			else if ((ch < 32) || (ch > 127))
			{
				buffer1.append((char) ch);
				buffer2.append((char) ch);
			}
			else
			{
				buffer1.append((char) ch);
				buffer2.append((char) ch);
			}
		}
		if (buffer2.length() > 0)
		{
			dumpStream("<< ", new ByteArrayInputStream(String.valueOf(buffer2).getBytes()));
		}
		return buffer1.toString();
	}

	public static void dump(String header, String buffer) throws IOException
	{
		dumpStream(header, new ByteArrayInputStream(buffer.getBytes()));
	}

	public static void dump(String header, byte[] b, int off, int len) throws IOException
	{
		if (b == null)
		{
			throw new IllegalArgumentException("Input may not be null");
		}
		dumpStream(header, new ByteArrayInputStream(b, off, len));
	}

	public static void dump(String header, byte[] b) throws IOException
	{
		if (b == null)
		{
			throw new IllegalArgumentException("Input may not be null");
		}
		dumpStream(header, new ByteArrayInputStream(b));
	}

}
